package com.filesys.servlet;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.webapp.WebAppContext;

import com.filesys.action.ListAction;
import com.filesys.core.IAction;
import com.filesys.core.util.IPUtils;
import com.server.core.IServer;
import com.server.core.ServerFactory;

public class FileSysServlet extends HttpServlet {

    private static final long                    serialVersionUID = 1L;
    
    private static final Map<String, IAction> map = new HashMap<String, IAction>();
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        map.put("/", new ListAction());
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String basePath = req.getServletPath();
        String reqpath = req.getPathInfo();
        reqpath = reqpath == null ? "/" : reqpath;
        System.out.println("access: " + reqpath);
        
        IAction action = map.get(reqpath);
        if(action == null){
            resp.sendRedirect(basePath);
        }else {
            action.doAction(req, resp);
        }
    }

    public static void main(String[] args) {
        int port = args != null && args.length > 0 ? Integer.valueOf(args[0]) : 8080;
        final IServer server = ServerFactory.getServer("web", port, "/", 0);
        printAddress(port, server);
        server.start();
        
    }

    private static void printAddress(int port, final IServer server) {
        Field f = null;
        try {
           f = server.getClass().getDeclaredField("webApp");
           f.setAccessible(true);
        } catch (NoSuchFieldException | SecurityException e) {
        }
        final Field field = f;
        new Thread(new Runnable() {

            int time = 5000;
            
            public void run() {
                int dur = 0;
                WebAppContext webApp = null;
                while (dur < time){
                    if(field != null){
                        if(webApp == null){
                            try {
                                webApp = (WebAppContext) field.get(server);
                            } catch (IllegalArgumentException | IllegalAccessException e) {
                            }
                        }
                        if(webApp != null && WebAppContext.STARTED.equals(webApp.getState())){
                            break;
                        }
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                    dur += 100;
                }
                List<String> addresses = IPUtils.getNetworkAddress();
                System.out.println("\nAddress : ");
                for (String ip : addresses) {
                    System.out.println("    http://" + ip + ":" + port + "/file");
                }
                System.out.println("");
            }
        }).start();
    }
}
